﻿using System.Collections.Generic;
using System.Linq;

using SeriesManager.Model.Banner;
using SeriesManager.Enum;
using Windows.UI.Xaml.Data;
using System;
using System.Runtime.Serialization;

namespace SeriesManager.ViewModel
{
    public class MediaViewModel : ViewModelBase
    {
        #region Properties

        public ICollectionView View
        {
            get;
            private set;
        }

        #endregion

        private void CurrentItem_Changed(object sender, object e)
        {
            var currentItem = (FanArtBannerModel)this.View.CurrentItem;
            if (currentItem != null)
            {
                base.NavigationService.Navigate<MediaDetailViewModel>(currentItem.ID);
            }
        }

        public override void OnNavigatedTo(object navigationParameter)
        {
            base.OnNavigatedTo(navigationParameter);

            var seriesModel = AppX.Instance.Current;
            this.View = new CollectionViewSource() { Source = seriesModel.FanArtBanners }.View;
            base.OnPropertyChanged<ICollectionView>(() => this.View);

            var currentItem = this.View.FirstOrDefault(B => (B as FanArtBannerModel).ID == seriesModel.FanArt.ID);
            if (currentItem != null)
            {
                this.View.MoveCurrentTo(currentItem);
            }

            this.View.CurrentChanged += CurrentItem_Changed;
        }
    }
}
